﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Jinx.Bindings.Observation;
using Jinx.Naming;
using Jinx.Naming.Paths;
using Jinx.Utility.Locking;

namespace Jinx.Bindings
{
    public class BindingContext : IEnumerable<IBinding>
    {
        private readonly IDictionary<Path, IBinding> Bindings;
        private readonly LockingContext lockingContext;
        private readonly NamingContext namingContext;

        public BindingContext()
        {
            Bindings = new Dictionary<Path, IBinding>();

            lockingContext = new LockingContext();
            namingContext = new NamingContext(LockingContext);
        }

        public NamingContext NamingContext
        {
            get { return namingContext; }
        }

        public LockingContext LockingContext
        {
            get { return lockingContext; }
        }

        public IBinding this[Path Path]
        {
            get
            {
                using (LockingContext.ReadLock)
                    return Bindings[Path];
            }
        }

        #region IEnumerable<IBinding> Members

        public IEnumerator<IBinding> GetEnumerator()
        {
            return Bindings.Values.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        public void AddBinding(IBinding Binding)
        {
            if (Invalid(Binding))
                Binding.Invalidated();

            using (LockingContext.WriteLock)
            {
                Bindings.Add(Binding.Path, Binding);

                new BindingObserver(Binding, this);
            }

            Binding.Bound(this);
        }

        public void RemoveBinding(IBinding Binding)
        {
            RemoveBinding(Binding.Path);
        }

        public void RemoveBinding(Path Path)
        {
            using (LockingContext.WriteLock)
                Bindings.Remove(Path);
        }

        public IEnumerable<IBinding> PrefixesFor(Path Path)
        {
            using (LockingContext.ReadLock)
            {
                return from Binding in Bindings.Values
                       where Binding.Path.IsPrefix(Path)
                       select Binding;
            }
        }

        public IEnumerable<IBinding> PrefixesOf(Path Path)
        {
            using (LockingContext.ReadLock)
            {
                return from Binding in Bindings.Values
                       where Path.IsPrefix(Binding.Path)
                       select Binding;
            }
        }

        private bool Invalid(IBinding Binding)
        {
            try
            {
                var O = new PathResolver(NamingContext).Resolve(Binding.Path);
                return (O == null);
            }
            catch (CouldNotResolvePath)
            {
                return true;
            }
        }
    }
}